﻿using Dapper_Core.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;
using Dapper;

namespace Dapper_Core.DAL.Impl
{
    public class ConnectionFactory
    {
        private static string _connectionString = Appsettings.SqlServerConnection01.Value;
        public ConnectionFactory()
        {

        }

        /// <summary>
        /// 获取数据库连接对象（连接已Open）
        /// </summary>
        /// <param name="mars"></param>
        /// <returns></returns>
        public static DbConnection GetOpenConnection(bool mars = false)
        {
            //设置系统默认数据库
            return GetOpenConnection(DBEnum.ADMP, mars);
        }

        /// <summary>
        /// 获取数据库连接对象（连接已Open）
        /// </summary>
        /// <param name="mars"></param>
        /// <returns></returns>
        public static DbConnection GetOpenConnection(DBEntity dbEntity, bool mars = false)
        {
            if (dbEntity.DBType == SimpleCRUD.Dialect.MySQL)
            {
                SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
                return GetOpenConnection_MySql(dbEntity, mars);
            }
            //默认返回SqlServer数据库连接对象
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.SQLServer);
            return GetOpenConnection_SqlServer(dbEntity, mars);
        }

        /// <summary>
        /// 获取SqlServer数据库连接对象（连接已Open）
        /// </summary>
        /// <param name="mars">true可以在一个连接中打开多个DataReader，false只能在一个连接中打开一个DataReader</param>
        /// <returns></returns>
        private static SqlConnection GetOpenConnection_SqlServer(DBEntity dbEntity, bool mars = false)
        {
            var cs = dbEntity.ConnectionString;
            if (mars)
            {
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(cs);
                scsb.MultipleActiveResultSets = true;
                cs = scsb.ConnectionString;
            }
            var connection = new SqlConnection(cs);
            connection.Open();
            return connection;
        }

        /// <summary>
        /// 获取MySql数据库连接对象（连接已Open）
        /// </summary>
        /// <param name="mars">true可以在一个连接中打开多个DataReader，false只能在一个连接中打开一个DataReader</param>
        /// <returns></returns>
        private static MySqlConnection GetOpenConnection_MySql(DBEntity dbEntity, bool mars = false)
        {
            var cs = dbEntity.ConnectionString;
            if (mars)
            {

            }
            var connection = new MySqlConnection(cs);
            connection.Open();
            return connection;
        }

        /// <summary>
        /// 获取数据库连接对象（连接已Close）
        /// </summary>
        /// <returns></returns>
        public static DbConnection GetClosedConnection(DBEntity dbEntity)
        {
            if (dbEntity.DBType == SimpleCRUD.Dialect.MySQL)
            {
                SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
                return GetClosedConnection_MySql(dbEntity);
            }
            //默认返回SqlServer数据库连接对象
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.SQLServer);
            return GetClosedConnection_SqlServer(dbEntity);
        }

        /// <summary>
        /// 获取SqlServer数据库连接对象（连接已Close）
        /// </summary>
        /// <returns></returns>
        private static SqlConnection GetClosedConnection_SqlServer(DBEntity dbEntity)
        {
            return new SqlConnection(dbEntity.ConnectionString);
        }

        /// <summary>
        /// 获取MySql数据库连接对象（连接已Close）
        /// </summary>
        /// <returns></returns>
        private static MySqlConnection GetClosedConnection_MySql(DBEntity dbEntity)
        {
            return new MySqlConnection(dbEntity.ConnectionString);
        }

        static void RunPerformanceTests()
        {
#if PERF
            var test = new PerformanceTests();
            const int iterations = 500;
            Console.WriteLine("Running {0} iterations that load up a post entity", iterations);
            test.Run(iterations);
#else
            Console.WriteLine("Performance tests have not been built; add the PERF symbol");
#endif
        }
    }
}
